<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
session_start();
$_SESSION["ID"] = 2;
$_SESSION["NAME"] = 'admin';
$_SESSION["USER"] = '1061133214@qq.com';
$app->get('/admin', function()
{
	$_SESSION = Session::all();
	if(!isset($_SESSION["id"]) || !isset($_SESSION["name"]))
		return view('login');
	return view('admin');
});
$app->get('/', function(){
	return view('index');
});

$app->get('modeler', function(){
	return view('modelList');
});

$app->get('help', function(){
	return view('help');
});

$app->get('invitation/{id}/edit', function($id) {

	if(!isset($_SESSION["ID"])) {
		Header("Location: http://chinalove99.net/index.php?c=main&a=user_login&redirect=".urlencode("http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]) );
		exit;
	}

	$isDemo = DB::table("model")
	->select("id")
	->where("demo_id",$id)
	->get();

	$result = DB::table("invitation")
		->join("model","model.id","=","invitation.model_id")
		->select("invitation.*",
			"model.category_id")
		->where("invitation.id",$id)
		->get();
	//var_dump($result);exit();
	$result = (array)$result[0];
// map_dir ,分开 （y，x）
	$pictures = DB::table("picture")
	->select("dir")
	->where("invitation_id",$id)
	->where("is_delete",0)
	->get();
	$result['pictures'] = $pictures;

	$music = DB::table("music")
	->select("singer","name")
	->where("id",$result['music_id'])
	->get();
	if (!empty($music)) {
		$music = (array)$music[0];
		$result['music'] = $music['singer']."-".$music['name'];
	}
	if (empty($isDemo)) {
		$result['isDemo'] = "false";
		return view('invitation.edit',$result);
	}else{
		$result['isDemo'] = "true";
		return view('invitation.edit',$result);
	}
});

$app->get('invitation/attend', function() {
   return view('invitation.attend');
});

$app->get('invitation/bless', function() {
   return view('invitation.bless');
});

$app->get('invitation/list', function() {
   return view('invitation.list');
});


$app->get('invitation/{id}', function($id) {
    $invitationResult = DB::select("SELECT invitation.*,music.dir AS music_id_dir 
       FROM invitation 
       LEFT JOIN music ON music.id = invitation.music_id
       WHERE invitation.id = $id");
    if(empty($invitationResult)){
      return "喜帖不存在。";
    }

    $invitationResult = (array)$invitationResult[0];
    $invitationResult["music_dir"] = ($invitationResult["music_id_dir"]!="")?$invitationResult["music_id_dir"]:$invitationResult["music_dir"];
    $invitationResult["pictures"] = DB::select("SELECT dir FROM picture
       WHERE invitation_id = $id
       AND is_visible = 1
       AND is_delete = 0
       ORDER BY weight DESC,created_at DESC");

	$modelResult =DB::select("SELECT model.dir_name FROM model 
		INNER JOIN invitation ON invitation.model_id = model.id
	    WHERE invitation.id = $id");
	$dirName = $modelResult[0]->dir_name;

    return view('model.'.$dirName.'.index',$invitationResult);
});

$app->get('invitation/{id}/thanks',function($id) {
    $invitationResult = DB::select("SELECT id,boy_name,girl_name,head_dir,thanks_letter,wedding_video_dir,music_dir
       FROM invitation 
       WHERE id = $id");
    if(empty($invitationResult)){
      return "喜帖不存在。";
    }

    $invitationResult = (array)$invitationResult[0];

	$modelResult =DB::select("SELECT model.dir_name FROM model 
		INNER JOIN invitation ON invitation.model_id = model.id
	    WHERE invitation.id = $id");
	$dirName = $modelResult[0]->dir_name;
    return view('model.'.$dirName.'.thanks',$invitationResult);
});


$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {

	//喜帖
	$api->get('v1/invitation',                       'App\Http\Controllers\InvitationController@index');
	$api->get('v1/invitation/{id}',                  'App\Http\Controllers\InvitationController@show');
	$api->post('v1/invitation',                      'App\Http\Controllers\InvitationController@store');
	$api->put('v1/invitation/{id}',                  'App\Http\Controllers\InvitationController@update');
	$api->delete('/invitation/{id}',               'App\Http\Controllers\InvitationController@destory');

	//祝福
	$api->get('bless',            					'App\Http\Controllers\BlessController@index');
	$api->post('v1/invitation/{id}/bless',           'App\Http\Controllers\BlessController@store');

	//出席
	$api->get('v1/attend',            				'App\Http\Controllers\AttendController@index');

	//模板
	$api->get('v1/model',                        		'App\Http\Controllers\ModelController@index');

	// 模板分类
	$api->get('v1/category',                       	'App\Http\Controllers\CategoryController@index');

	//音乐
	$api->get('v1/music',                       		'App\Http\Controllers\MusicController@index');

	//获取七牛uptoken
	$api->get  ('/qiniu/token',                      	    'App\Http\Controllers\QiniuController@token');
	$api->post  ('/qiniu/makeSaveasUrl',                    'App\Http\Controllers\QiniuController@makeSaveasUrl');

	//通用
	$api->get 	('/{table:[A-Z_a-z]+}/config', 				'App\Http\Controllers\ApiController@config'		);

	$api->post 	('/file', 					                'App\Http\Controllers\ApiController@upload'		);
	$api->get 	('/{table:[A-Z_a-z]+}', 					'App\Http\Controllers\ApiController@index'		);
	$api->get 	('/{table:[A-Z_a-z]+}/{id:[0-9]+}', 		'App\Http\Controllers\ApiController@show'		);
	$api->post 	('/{table:[A-Z_a-z]+}', 					'App\Http\Controllers\ApiController@store'		);
	$api->put 	('/{table:[A-Z_a-z]+}/{id:[0-9]+}', 		'App\Http\Controllers\ApiController@update'		);
	$api->delete('/{table:[A-Z_a-z]+}/{id:[0-9]+}', 		'App\Http\Controllers\ApiController@destroy'	);
	$api->get 	('/config/init', 							'App\Http\Controllers\ConfigController@init'	);

	$api->get  ('/user/session',               				 'App\Http\Controllers\UserController@check');
	$api->post ('/user/login',                				 'App\Http\Controllers\UserController@login');
	$api->get  ('/user/logout',                				 'App\Http\Controllers\UserController@logout');
	$api->post ('/user/{id}/password',        				 'App\Http\Controllers\UserController@password');	
});

